<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:jsd="http://www.cycle99.com/tools/ecmadoc"
	xmlns:math="http://www.cycle99.com/tools/ecmadoc/transform/math"
	xmlns:help="http://www.cycle99.com/tools/ecmadoc/transform/help"
	xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="jsd math help">

	<xsl:include href="common.xslt"/>

	<xsl:template match="/" mode="head-current">
		<script type="text/javascript" language="javascript" src="scripts/toggle.js"></script>
	</xsl:template>

	<xsl:template match="/" mode="content">
		<xsl:apply-templates select="$transformSubject" mode="object"/>
	</xsl:template>

	<xsl:template match="*" mode="object">
		<div class="{name()}">
			<xsl:apply-templates select="." mode="object-head"/>
			<div class="content top">
				<xsl:apply-templates select="." mode="object-body"/>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="*" mode="object-head">
		<xsl:apply-templates select="." mode="header-top"/>
	</xsl:template>

	<xsl:template match="*" mode="object-body">
		<div class="documentation">
			<xsl:apply-templates select="." mode="object-documentation"/>
		</div>
		<xsl:apply-templates select="." mode="object-member-summary"/>
		<xsl:if test="$singlePage = 'true'">
			<xsl:apply-templates select="." mode="object-member-details"/>
		</xsl:if>
	</xsl:template>

	<xsl:template match="*" mode="object-documentation">
		<xsl:apply-templates select="." mode="summary"/>
		<xsl:apply-templates select="." mode="syntax"/>
		<xsl:apply-templates select="." mode="arguments"/>
		<xsl:apply-templates select="." mode="returns"/>
		<xsl:apply-templates select="." mode="inheritance"/>
		<xsl:apply-templates select="." mode="description"/>
		<xsl:apply-templates select="." mode="examples"/>
	</xsl:template>

	<xsl:template match="*[property[@name='example']]" mode="examples">
		<h4 class="togglehead">
			<div class="switch"></div>
			<div class="title">
				<xsl:value-of select="help:Iif(count(property[@name='example']) = 1, 'Example', 'Examples')"/>
			</div>
		</h4>
		<div class="toggledetail">
			<xsl:apply-templates select="property[@name='example']"/>
		</div>
	</xsl:template>

	<xsl:template match="property[@name='syntax']">
		<xsl:value-of select="help:ColorCodeJavaScript(.)" disable-output-escaping="yes"/>
		<br/>
	</xsl:template>

	<xsl:template match="property[@name='example']">
		<xsl:variable name="lang" select="@lang"/>
		<xsl:variable name="comment" select="@comment"/>
		<div class="example">
			<xsl:if test="$comment">
				<p class="comment">
					<xsl:value-of select="$comment" disable-output-escaping="yes"/>
				</p>
			</xsl:if>
			<xsl:element name="div">
				<xsl:attribute name="class">code</xsl:attribute>
				<xsl:value-of select="help:ColorCode(., $lang)" disable-output-escaping="yes"/>
			</xsl:element>
		</div>
	</xsl:template>

	<xsl:template match="*" mode="syntax">
		<xsl:if test="count(property[@name='syntax']) != 0 or string-length(@synopsisExtended) != 0">
			<div class="toggleblock stx">
				<h4 class="togglehead">
					<div class="switch"></div>
					<div class="title">
						Syntax
					</div>
				</h4>
				<div class="toggledetail">
					<div class="synopsis">
						<xsl:choose>
							<xsl:when test="count(property[@name='syntax']) != 0">
								<xsl:apply-templates select="property[@name='syntax']"/>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="@synopsisExtended" disable-output-escaping="yes"/>
							</xsl:otherwise>
						</xsl:choose>
					</div>
					<xsl:apply-templates select="." mode="value"/>
				</div>
			</div>
		</xsl:if>
	</xsl:template>

	<xsl:template match="property | variable" mode="value">
		<div class="propertyvalue">
			<div class="type">
				<label>Type:</label>
				<span class="value">
					<xsl:apply-templates select="." mode="type-link"/>
				</span>
			</div>
			<div class="memberof">
				<label>Member of:</label>
				<span class="value">
					<xsl:apply-templates select="parent::*/parent::*/parent::*" mode="link"/>
				</span>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="*[arguments/argument or arguments/variable]" mode="arguments">
		<div class="toggleblock args">
			<h4 class="togglehead">
				<div class="switch"></div>
				<div class="title">
					Arguments
				</div>
			</h4>
			<div class="toggledetail">
				<div class="arguments">
					<xsl:for-each select="arguments/argument | arguments/variable">
						<div class="argument">
							<xsl:choose>
								<xsl:when test="@range">
									<span class="name">[<xsl:value-of select="@range"/>]<label class="colon">:</label></span>
								</xsl:when>
								<xsl:otherwise>
									<span class="name"><xsl:value-of select="@name"/><label class="colon">:</label></span>
								</xsl:otherwise>
							</xsl:choose>
							<span class="type">
								<xsl:apply-templates select="." mode="type-link"/>
							</span>
							<div class="description">
								<xsl:value-of select="@description" disable-output-escaping="yes"/>
							</div>
						</div>
					</xsl:for-each>
				</div>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="*[returns]" mode="returns">
		<div class="toggleblock rtrn">
			<h4 class="togglehead">
				<div class="switch"></div>
				<div class="title">
					Return value
				</div>
			</h4>
			<div class="toggledetail">
				<div class="returns">
					<div class="type">
						<xsl:apply-templates select="returns" mode="type-link"/>
					</div>
					<div class="description">
						<xsl:value-of select="returns/@description" disable-output-escaping="yes"/>
					</div>
				</div>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="class[extends and not(@static = 'true')]" mode="inheritance">
		<div class="toggleblock ext">
			<h4 class="togglehead">
				<div class="switch"></div>
				<div class="title">
					Inheritance
				</div>
			</h4>
			<div class="toggledetail">
				<div class="inheritance">
					<xsl:apply-templates select="extends/level[1]" mode="inheritance"/>
				</div>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="extends/level" mode="inheritance">
		<div class="level">
			<xsl:for-each select="type">
				<xsl:apply-templates select="." mode="link"/>
				<xsl:if test="position() != last()">
					<xsl:text>, </xsl:text>
				</xsl:if>
			</xsl:for-each>
			<xsl:choose>
				<xsl:when test="following-sibling::level[1]">
					<xsl:apply-templates select="following-sibling::level[1]" mode="inheritance"/>
				</xsl:when>
				<xsl:otherwise>
					<div class="level current">
						<span><xsl:value-of select="parent::extends/parent::*/@name"/></span>
					</div>
				</xsl:otherwise>
			</xsl:choose>
		</div>
	</xsl:template>

	<xsl:template match="*" mode="object-member-summary">
		<xsl:if test="count(members/*/*) != 0">
			<div class="member-summary">
				<xsl:for-each select="members/*">
					<xsl:apply-templates select="." mode="member-overview"/>
				</xsl:for-each>
			</div>
		</xsl:if>
	</xsl:template>

	<xsl:template match="*" mode="object-member-details">
		<xsl:if test="count(members/*/*) != 0">
			<div class="member-details">
				<xsl:for-each select="members/*">
					<xsl:apply-templates select="." mode="member-group"/>
				</xsl:for-each>
			</div>
		</xsl:if>
	</xsl:template>

	<xsl:template match="*" mode="member-group">
		<xsl:param name="members" select="*"/>
		<xsl:param name="item-type" select="help:ToUpperCaseFirst(name($members[1]))"/>
		<div class="toggleblock dtl">
			<h4 class="togglehead">
				<div class="switch"></div>
				<div class="title">
					<xsl:value-of select="$item-type"/> detail
				</div>
			</h4>
			<div class="toggledetail">
				<xsl:apply-templates select="$members" mode="member-detail">
					<xsl:sort select="@sort" data-type="number" order="ascending"/>
					<xsl:sort select="@name" data-type="text" order="ascending" />
				</xsl:apply-templates>
			</div>
		</div>
	</xsl:template>

	<xsl:template match="*" mode="member-detail">
		<div class="member">
			<a name="{@name}" class="anchor"></a>
			<h3>
				<xsl:apply-templates select="." mode="icon"/>
				<div class="text">
					<xsl:value-of select="help:Iif(@visibility = 'static', @staticName, @name)"/>
				</div>
			</h3>
			<div class="content">
				<xsl:apply-templates select="." mode="object-body"/>
			</div>
		</div>
	</xsl:template>

</xsl:stylesheet>
